﻿using ChurchDiary.BLL;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ChurchWebsite.Areas.CommonDataService.Controllers
{
    public class DataServiceController : Controller
    {
        //
        // GET: /CommonDataService/DataService/
        public JsonResult GetRelationship()
        {
            return Json(new BLL_Shared().GetRelationShip(),JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetRelationshipByStatus(bool IsMarried)
        {
            if (IsMarried == true)
            {
                return Json(new BLL_Shared().GetRelationShip(), JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new BLL_Shared().GetUnMarriedRelationShip(), JsonRequestBehavior.AllowGet);
            }
        }
        public ActionResult GetMinitrialType()
        {
            return Json(new BLL_Organization().GetDepartments(),JsonRequestBehavior.AllowGet);
        }
        public ActionResult GetChurchDesignation()
        {
            return Json(new BLL_Church().GetChurchDesignation(), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetChurchZones(int OrganizationId)
        {
            return Json(new BLL_Organization().GetOrganizationPartitions(OrganizationId), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetDesignations(int OrganizationId)
        {
            return Json(new BLL_Church().GetDesignations(OrganizationId), JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetGrade()
        {
            return Json(new BLL_Shared().GetGrageSettings(), JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public JsonResult Uploadfile()
        {
            if (Request.Files.Count > 0)
            {
                var file = Request.Files[0];

                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/uploads/uploaded/"), fileName);
                    file.SaveAs(path);
                }
            }
            return Json("");
        }
        [HttpPost]
        public JsonResult UploadBannerImage()
        {
            string fileName = "";
            if (Request.Files.Count > 0)
            {
                var file = Request.Files[0];

                if (file != null && file.ContentLength > 0)
                {
                    var extention = Path.GetExtension(file.FileName);
                    Guid Name = Guid.NewGuid();
                    fileName = Name + extention;
                    string folder = Createfolder("BannerImages");
                    var path = Path.Combine(folder, fileName);
                    file.SaveAs(path);
                }
            }
            return Json(fileName);
        }
        [HttpPost]
        public JsonResult UploadArticleImage()
        {
            string fileName = "";
            if (Request.Files.Count > 0)
            {
                var file = Request.Files[0];

                if (file != null && file.ContentLength > 0)
                {
                    var extention = Path.GetExtension(file.FileName);
                    Guid Name = Guid.NewGuid();
                    fileName = Name + extention;
                    string folder = Createfolder("ArticleImage");
                    var path = Path.Combine(folder, fileName);
                    file.SaveAs(path);
                }
            }
            return Json(fileName);
        }
        [HttpPost]
        public JsonResult UploadMagazine()
        {
            string fileName = "";
            if (Request.Files.Count > 0)
            {
                var file = Request.Files[0];

                if (file != null && file.ContentLength > 0)
                {
                    var extention = Path.GetExtension(file.FileName);
                    Guid Name = Guid.NewGuid();
                    fileName = Name + extention;
                    string folder = Createfolder("Magazine/IndexImages");
                    var path = Path.Combine(folder, fileName);
                    file.SaveAs(path);
                }
            }
            return Json(fileName);
        }
        [HttpPost]
        public JsonResult UploadMemberShipforms()
        {
            string fileName = "Nofile";
            if (Request.Files.Count > 0)
            {
                var file = Request.Files[0];

                if (file != null && file.ContentLength > 0)
                {
                    var extention = Path.GetExtension(file.FileName);
                    Guid Name = Guid.NewGuid();
                    fileName = Name + extention;
                    string folder = Createfolder("Documents/Membershipforms");
                    var path = Path.Combine(folder, fileName);
                    file.SaveAs(path);
                }
            }
            return Json(fileName);
        }
        public string Createfolder(string folderName)
        {
            string Path = Server.MapPath("~/uploads/" + folderName);
            bool exists = System.IO.Directory.Exists(Path);
            if (!exists)
            {
                System.IO.Directory.CreateDirectory(Path);
            }
            return Path;
        }
        public JsonResult GetNotification()
        {
            return Json(new BLL_Settings().GetNotifications(), JsonRequestBehavior.AllowGet);
        }
        
    }
}
